package cn.ges.movie.controller.system

import cn.ges.base.sta.constant.StaBaseConstant
import com.ges.common.anno.DataBuryPoint
import com.ges.common.service.config.ConfigService
import org.springframework.context.annotation.Lazy
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
import javax.annotation.Resource
import javax.servlet.http.HttpServletResponse

/**
 *
 * @author gespent@163.com
 * @date 2021/4/16 11:02
 */
@RestController
open class ConfigV2Controller {


    @Lazy
    @Resource
    lateinit var configService: ConfigService



    @DataBuryPoint("获得菜单V2")
    @RequestMapping(value = ["/v2/auth/menu/"], method = [RequestMethod.GET])
    open fun getUserMenu(res: HttpServletResponse) {
        res.addHeader("Content-Type", "application/json;charset=UTF-8")
        val context = SecurityContextHolder.getContext()
        if (context != null) {
            val authentication = context.authentication
            val authorities = authentication.authorities
            authorities.forEach {
                val stringValue = when (it.authority) {
                    "ALL" -> configService.getStringValue(9998, "[]")
                    "BG_ADMIN" -> configService.getStringValue(9997, "[]")
                    StaBaseConstant.BG_USER_AUTH -> configService.getStringValue(9999, "[]")
                    else -> "[]"
                }
                res.writer.println(stringValue)
            }
        } else {
            res.writer.println("[]")
        }
    }

}